#!/usr/bin/python
"test module for the edendb.py module"


def make_test_db():
    "create the test database in memory"
    import sqlite3
    conn = sqlite3.connect(":memory:")
    cur = conn.cursor()
    cur.execute("create table test (name varchar, age integer)")
    for item in range(10):
        sql = "insert into test (name, age) values ('paul%s', %s)" % (item,
            item)
        cur.execute(sql)
    conn.commit()
    cur.close()
    return conn


def test(options):
    import doctest
    import edendb
    conn = make_test_db()
    if options.coverage:
        import coverage
        coverage.erase()
        coverage.start()
        edendb = reload(edendb)
    doctest.testmod(edendb, verbose=options.verbose,
        extraglobs={'db':edendb.EdenDB(conn)})
    conn.close()
    if options.coverage:
        coverage.stop()
        coverage.report(['edendb.py'])


def main():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-v", "--verbose", action="store_true",
        help="when running tests, run gregariously")
    parser.add_option("-c", "--coverage", action="store_true",
        help="after running tests, show code coverage (just for edendb module)")
    (options, args) = parser.parse_args()
    test(options)

if __name__ == "__main__":
    main()
